<?php
/**
 * ################################################
 * #【Description】 商品接口，用户小程序商品数据调用
 * #【Author】 akon
 * #【Date】 2017年6月5日
 * ################################################
 */
namespace Shx\Controller;
use Think\Controller;

class GoodController extends Controller {

    /**
     * 获取商品数据接口
     */
    /*public function getGoods(){
        $sql = "SELECT
                a.goods_id,a.cat_id,a.brand_id,a.brand,a.thumbnail_pic,a.small_pic,a.big_pic,b.cat_name,c.source
                FROM
                (sdb_goods_cat b
                JOIN
                sdb_goods a
                ON b.cat_id=a.cat_id)
                JOIN sdb_gimages c
                ON a.goods_id = c.goods_id
                LIMIT 100";

        $goodsInfo = M()->query($sql);

        ###################################数据重组###################################
        foreach($goodsInfo as $k=>$v){
            $thumb = explode('|',$v['thumbnail_pic']);
            array_pop($thumb);
            $goodsInfo[$k]['thumbnail_pic'] = $thumb;

            $small_pic = explode('|',$v['small_pic']);
            array_pop($small_pic);
            $goodsInfo[$k]['small_pic'] = $small_pic;

            $big_pic = explode('|',$v['big_pic']);
            array_pop($big_pic);
            $goodsInfo[$k]['big_pic'] = $big_pic;
        }

        foreach($goodsInfo as $k=>$v){
            $goods[$v['goods_id']]['goods_id'] = $v['goods_id'];
            $goods[$v['goods_id']]['source'][] = $v['source'];
            $goods[$v['goods_id']]['cat_id'] = $v['cat_id'];
            $goods[$v['goods_id']]['cat_name'] = $v['cat_name'];
            $goods[$v['goods_id']]['brand_id'] = $v['brand_id'];
            $goods[$v['goods_id']]['brand'] = $v['brand'];
            $goods[$v['goods_id']]['thumbnail_pic'] = $v['thumbnail_pic'][0];
            $goods[$v['goods_id']]['small_pic'] = $v['small_pic'][0];
            $goods[$v['goods_id']]['big_pic'] = $v['big_pic'][0];
            $goods[$v['goods_id']]['source'][] = $v['source'];
        }
        $goods = array_values($goods);
        ###################################数据重组###################################
        echo json_encode($goods);
    }*/

    ##################################首页商品数据接口##################################
    public function getGoods(){
        $sql = "SELECT
                a.goods_id,a.cat_id,a.brand_id,a.brand,a.thumbnail_pic,a.small_pic,a.big_pic,b.cat_name,c.source
                FROM
                (shx_goods_cat b
                JOIN
                sdb_goods a
                ON b.cat_id=a.cat_id)
                JOIN shx_gimages c
                ON a.goods_id = c.goods_id
                LIMIT 100";
        $sql = "SELECT goods_id,name,intro,price,small_pic FROM shx_goods limit 100";

        $goodsInfo = M()->query($sql);
        ##数据重组##
        foreach($goodsInfo as $k=>$v){

            $small_pic = explode('|',$v['small_pic']);
            array_pop($small_pic);
            $goodsInfo[$k]['small_pic'] = 'http://www.shenhexin.com/'.$small_pic[0];

        }
        ##数据重组##
        echo json_encode($goodsInfo);
    }
    ##################################首页商品数据接口##################################

    ##################################商品详情接口##################################
    public function getGoodsDetail(){
        $goods_id = I('goods_id');
        $sql = "SELECT a.goods_id,a.name,a.price,a.intro,a.store,b.source FROM shx_goods a JOIN shx_gimages b ON a.goods_id = b.goods_id WHERE a.goods_id =" . $goods_id;

        $goodsInfo = M()->query($sql);

        foreach($goodsInfo as $key=>$val){
            $val['source'] = "http://www.shenhexin.com/home/upload/" . $val['source'];
            $goods[$val['goods_id']]['goods_id'] = $val['goods_id'];
            $goods[$val['goods_id']]['name'] = $val['name'];
            $goods[$val['goods_id']]['price'] = $val['price'];
            $goods[$val['goods_id']]['store'] = $val['store'];
            $goods[$val['goods_id']]['source'][] = $val['source'];
            //正则提取详情图片src
            $pattern = '/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(?<imgsrc>.*?)\\1[^>]*?\/?\s*>/i';
            preg_match_all($pattern,$val['intro'],$src);
            $goods[$val['goods_id']]['intro'] = $src['imgsrc'];
        }
        $goods = array_values($goods)[0];
        echo json_encode($goods);
    }
    ##################################商品详情接口##################################


    /**
     * 保存登录用户信息
     */
    public function saveUserInfo(){
        $userInfo = I('userInfo');
        $openId = I('openId');
        echo $openId;
        $userInfo = json_decode(htmlspecialchars_decode($userInfo),true);  //将用户信息转换为数组
        //查询数据表中是否有此用户
        $res = M('user')->where(array('open_id'=>$openId))->count();
        if(!$res){
            $info = array(
                'open_id' => $openId,
                'nickname' => $userInfo['nickName'],
                'avatarUrl' => $userInfo['avatarUrl']
            );
            $id = M('user')->add($info);
            if($id){
                echo $id;
            }
        }else{
            echo "用户已存在";
        }

    }


    /**
     * 解密openId
     * 1、获取code
     * 2、code换取session_key
     * 3、
     */
    public function getOpenId(){
        $code = I('code');
        $encryptOpenId = I('encryptOpenId');
        $userInfo = json_decode(htmlspecialchars_decode(I('userInfo')),true);

        $session_key_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.C('APPID').'&secret='.C('APPSECRET').'&js_code='.$code.'&grant_type=authorization_code';

        echo $code;

    }

}